package com.baomidou.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.springboot.entity.Cart;
import com.baomidou.springboot.entity.CartItem;
import com.baomidou.springboot.mapper.CartItemMapper;
import com.baomidou.springboot.mapper.CartMapper;
import com.baomidou.springboot.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private CartItemMapper cartItemMapper;

    @Override
    public Cart getCartById(Long id) {
        return cartMapper.selectById(id);
    }

    @Override
    public List<CartItem> getCartItemsByCartId(Long cartId) {
        QueryWrapper<CartItem> wrapper = new QueryWrapper<>();
        wrapper.eq("cart_id", cartId).isNull("delete_at");
        return cartItemMapper.selectList(wrapper);
    }

    @Override
    public boolean addCartItem(CartItem cartItem) {
        return cartItemMapper.insert(cartItem)!=0;
    }

    @Override
    public boolean updateCartItem(CartItem cartItem) {
        return cartItemMapper.updateById(cartItem)!=0;
    }

    @Override
    public boolean deleteCartItemById(Long id) {
        return cartItemMapper.deleteById(id)!=0;
    }

    @Override
    public boolean deleteCartById(Long id) {
        Cart cart = new Cart();
        cart.setId(id);
        cart.setDeleteAt(LocalDateTime.now());
        return cartMapper.updateById(cart)!=0;
    }
}
